package com.imcoke.abchat.aspect;

import com.imcoke.abchat.common.ResultEnum;
import com.imcoke.abchat.domain.ResultBody;
import com.imcoke.abchat.common.utils.FormValidationUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * @Author ccoke
 * @Description 用于验证表单是否完整
 * @Date: 14:25 2017-11-28
 */
@Aspect
@Component
@Order(1)
public class FormValidationAspect {
    @Autowired
    private FormValidationUtil formValidationUtil;

    //get,post请求
//    @Pointcut("execution(* com.imcoke.abchat.web.controller.*.p*(..))")
    @Pointcut("@annotation(com.imcoke.abchat.common.annotation.FormValidation)")
    public void formPointcut(){}

    @Around("formPointcut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        //开始
        Object arg = pjp.getArgs()[0];
        if (!formValidationUtil.checkForm(arg)){
            //如果参数不满足，则返回错误
            return ResponseEntity.ok(new ResultBody(ResultEnum.PARAMETER_MISS,""));
        }
        //执行方法
        return pjp.proceed(pjp.getArgs());
    }


}
